#ifndef CDEVICECTRL_H
#define CDEVICECTRL_H

#include <pthread.h>
#include "global.h"
#include "log.h"
#include "libv4l2.h"
#include "libv4lconvert.h"
#include "string"


using namespace std;

struct VideoBuffer {
        void   *start;
        size_t length;
};

class CDeviceCtrl
{
public:
    explicit CDeviceCtrl(void *parent = 0);
    ~CDeviceCtrl();

    int OpenDevice();
    int ReadAndDisplay();
    int CloseDevice();
    int GetDeviceHandle() {return m_hDeviceHandle;}
    struct VideoBuffer *GetVideoBuffer(){return video_buffers;}
    bool GetCapFlag() {return m_bCaptureFlag;}
    PRealMedia GetMedia(){return pMedia;}

protected:
    PRealMedia MediaInit();
    void MediaUnInit();

    friend void *ThreadVideoCapture(void *pVoid);
    friend void *ThreadVideoEncode(void *pVoid);

private:
    void *parent;
    pthread_t m_hThreadVideoCapture, m_hThreadVideoEncode;

    bool m_bCaptureFlag;
    int  m_hDeviceHandle;
    struct VideoBuffer *video_buffers;

    uint32_t  m_BufferCount, m_InputIndex, m_VideoFrameRate, m_CaptureWidth,
                m_CaptureHeight, m_Volume;

    PRealMedia pMedia;
};

#endif // CDEVICECTRL_H
